
#ifndef UCDOCMAN_H
#define UCDOCMAN_H

#include "zadesk.h"
#include "zaccoredefs.h"
#ifdef _ZSOFT_WINDOWS_
#include <wtypes.h>  
#endif

#include "zacdb.h"
#pragma pack (push, 8)

class UcTransactionManager;
class UcDbDatabase;
class CDocument;
class UcEdInputPointManager;
class UcApStatusBar;
class UcApPlotReactor;
class UcRxIterator;

struct IDispatch;

#define UC_DOCUMENT_MANAGER_OBJ _T("UcApDocManager")

struct UcAp {
    enum DocLockMode   {   kNone               = 0x00,
                           kAutoWrite          = 0x01,
                           kNotLocked          = 0x02,
                           kWrite              = 0x04,
                           kProtectedAutoWrite = 0x14,
                           kRead               = 0x20,
                           kXWrite             = 0x40  };
};

class UcLispAppInfo
{
public:
    ZTCHAR appFileName[_MAX_PATH];
    bool bUnloadable;
};

class ZSOFT_NO_VTABLE UcApDocument : public UcRxObject
{
public:
    UCRX_DECLARE_MEMBERS(UcApDocument);

    virtual const ZTCHAR*       fileName() const = 0;

    virtual CDocument* cDoc() const = 0;

    virtual UcTransactionManager* transactionManager() const = 0;

    virtual UcDbDatabase*     database() const = 0;

     enum SaveFormat {
        kUnknown = -1,      
        kR12_dxf = 1,       

        kR13_dwg = 4,       
        kR13_dxf = 5,       

        kR14_dwg = 8,       
        kR14_dxf = 9,       

        k2000_dwg = 12,      
        k2000_dxf = 13,      
        k2000_Template = 14, 
        k2000_Standard = 15, 
        k2000_xml = 16,      

        k2004_dwg = 24,      
        k2004_dxf = 25,      
        k2004_Template = 26, 
        k2004_Standard = 27,

		k2007_dwg = 36,      
		k2007_dxf = 37,      
		k2007_Template = 38, 
		k2007_Standard = 39, 
        k2010_dwg = 48,      
		k2010_dxf = 49,      
		k2010_Template = 50, 
		k2010_Standard = 51, 

		k2013_dwg = 60,      
		k2013_dxf = 61,      
		k2013_Template = 62, 
		k2013_Standard = 63, 

		k2018_dwg = 64,
		k2018_dxf = 65,
		k2018_Template = 66,
		k2018_Standard = 67,

		kNative = k2018_dwg,

        kR15_dwg = k2000_dwg,
        kR15_dxf = k2000_dxf,
        kR15_Template = k2000_Template,
        kR15_Standard = k2000_Standard,
        kR15_xml = k2000_xml,

        kR18_dwg = k2004_dwg,
        kR18_dxf = k2004_dxf,
        kR18_Template = k2004_Template,
		kR18_Standard = k2004_Standard,

		kR21_dwg = k2007_dwg,
		kR21_dxf = k2007_dxf,
		kR21_Template = k2007_Template,
		kR21_Standard = k2007_Standard,

		kR24_dwg = k2010_dwg,
		kR24_dxf = k2010_dxf,
		kR24_Template = k2010_Template,
		kR24_Standard = k2010_Standard,

		kR27_dwg = k2013_dwg,
		kR27_dxf = k2013_dxf,
		kR27_Template = k2013_Template,
		kR27_Standard = k2013_Standard,

		kR32_dwg = k2018_dwg,
		kR32_dxf = k2018_dxf,
		kR32_Template = k2018_Template,
		kR32_Standard = k2018_Standard
     };

    virtual SaveFormat formatForSave() const = 0;

	UCCORE_PORT static Ucad::ErrorStatus getDwgVersionFromSaveFormat(
		SaveFormat docFormat,
		UcDb::UcDbDwgVersion& dwgVer,
		UcDb::MaintenanceReleaseVersion& dwgMaintVer);

    virtual UcAp::DocLockMode lockMode(bool bIncludeMyLocks =
                                           false) const = 0;
    virtual UcAp::DocLockMode myLockMode() const = 0;

    virtual bool    isQuiescent() const = 0;

    virtual void * contextPtr() const = 0;

    virtual UcEdInputPointManager* inputPointManager() = 0;

    virtual const ZTCHAR * docTitle() const = 0;
    virtual IDispatch * GetIDispatch(bool bAddRef) = 0;
    virtual bool isReadOnly() const = 0;

    virtual int GetCountOfLispList() const = 0;
    virtual UcLispAppInfo* GetItemOfLispList(int nIndex) const = 0;

    virtual UcApStatusBar* drawingStatusBar() const = 0;
	virtual bool isCommandInputInactive() const = 0;

    void              pushDbmod();
    Ucad::ErrorStatus popDbmod();
};

class ZSOFT_NO_VTABLE UcApDocManagerReactor : public UcRxObject
{
public:
    UCRX_DECLARE_MEMBERS(UcApDocManagerReactor);

    virtual void              documentCreateStarted(UcApDocument* pDocCreating);
    
    virtual void              documentCreated(UcApDocument* pDocCreating);
    
    virtual void              documentToBeDestroyed(UcApDocument* pDocToDestroy);
    
    virtual void              documentDestroyed(const ZTCHAR* fileName);

    virtual void              documentCreateCanceled(UcApDocument* pDocCreateCancelled);

    virtual void              documentLockModeWillChange(UcApDocument* ,
                                         UcAp::DocLockMode myCurrentMode,
                                         UcAp::DocLockMode myNewMode,
                                         UcAp::DocLockMode currentMode,
                                         const ZTCHAR* pGlobalCmdName);
    virtual void              documentLockModeChangeVetoed(UcApDocument* ,
                                         const ZTCHAR* pGlobalCmdName);
    virtual void              documentLockModeChanged(UcApDocument* ,
                                         UcAp::DocLockMode myPreviousMode,
                                         UcAp::DocLockMode myCurrentMode,
                                         UcAp::DocLockMode currentMode,
                                         const ZTCHAR* pGlobalCmdName);

    virtual void              documentBecameCurrent(UcApDocument* );

    virtual void              documentToBeActivated(UcApDocument* pActivatingDoc );
    
    virtual void              documentToBeDeactivated(UcApDocument* pDeActivatedDoc );

    virtual void              documentActivationModified(bool bActivation);

    virtual void              documentActivated(UcApDocument* pActivatedDoc);
protected:
    UcApDocManagerReactor() : mVeto(0) {}
    Ucad::ErrorStatus         veto();

private:
    ZSoft::Int8               mVeto;

friend class UcApDocImpManager;
};

class ZSOFT_NO_VTABLE UcApDocumentIterator : public UcRxObject
{
public:
    UCRX_DECLARE_MEMBERS(UcApDocumentIterator);

    virtual bool    done() const = 0;
    virtual void              step() = 0;
    virtual UcApDocument*       document() = 0;
};

class ZSOFT_NO_VTABLE UcApDocManager : public UcRxObject
{
public:
    UCRX_DECLARE_MEMBERS(UcApDocManager);

    virtual UcApDocument*       curDocument() const = 0;
    virtual UcApDocument*       mdiActiveDocument() const = 0;

    virtual bool    isApplicationContext() const = 0;

    virtual UcApDocument*       document(const UcDbDatabase* ) const = 0;

    virtual Ucad::ErrorStatus lockDocument(UcApDocument* pDoc,
                                   UcAp::DocLockMode = UcAp::kWrite,
                                   const ZTCHAR* pGlobalCmdName = NULL,
                                   const ZTCHAR* pLocalCmdName = NULL,
                                   bool prompt = true) = 0;
    virtual Ucad::ErrorStatus unlockDocument(UcApDocument* pDoc) = 0;

    virtual UcApDocumentIterator* newUcApDocumentIterator() = 0;

    virtual void              addReactor(UcApDocManagerReactor* ) = 0;
    virtual void              removeReactor(UcApDocManagerReactor* ) = 0;

    virtual Ucad::ErrorStatus setDefaultFormatForSave(
                                UcApDocument::SaveFormat format) = 0;

	virtual UcApDocument::SaveFormat defaultFormatForSave() const = 0;

    virtual Ucad::ErrorStatus setCurDocument(UcApDocument* pDoc,
                                   UcAp::DocLockMode = UcAp::kNone,
                                   bool activate = false) = 0;
    
    virtual Ucad::ErrorStatus activateDocument(UcApDocument* pAcTargetDocument,
                                               bool bPassScript = false) = 0;

    virtual Ucad::ErrorStatus sendStringToExecute(UcApDocument* pAcTargetDocument,
                                       const ZTCHAR * pszExecute,
                                       bool bActivate = true,
                                       bool bWrapUpInactiveDoc = false,
                                       bool bEchoString = true) = 0;

    virtual Ucad::ErrorStatus appContextNewDocument(const ZTCHAR *pszTemplateName) = 0;
    virtual Ucad::ErrorStatus appContextOpenDocument(const ZTCHAR *pszDrawingName) = 0;
	virtual Ucad::ErrorStatus appContextRecoverDocument(const ZTCHAR *pszDrawingName) = 0;

    struct DocOpenParams {
        union { 
            const ZTCHAR * mpszFileName;
            const wchar_t * mpwszFileName;
        };

        union { 
            const ZTCHAR * mpszViewName;
            const wchar_t * mpwszViewName;
            const double * mpCoords;
        };
        enum {kUnknown = 0,             
              kDefaultView = 1,         
              kLayoutName = 2,          
              kViewName = 3,            
              kLayoutHandle = 4,        
              kViewHandle = 5,          
              kViewWorldCoords = 6};

        BYTE mnInitialViewType;
        enum {  kDisallowReadOnly       = 0x01,
                kRequireReadOnly        = 0x02,
                kFileNameArgIsUnicode   = 0x04,
                kViewNameArgIsUnicode   = 0x08,
                kUseUIOnErrors          = 0x10,
				kOwnerControlled        = 0x20,
				kUseUI                  = 0x40};

        int  mnFlags;       
        const wchar_t * mpwszPassword;
		void*			mpOwnerData;
        void*			mpUnused;
        int				mnUnused;
		const wchar_t*	mpwszTitle;
    };

	virtual Ucad::ErrorStatus appContextOpenDocument(const DocOpenParams *pParams) = 0;
    virtual Ucad::ErrorStatus newDocument() = 0;
    virtual Ucad::ErrorStatus openDocument() = 0;
    virtual Ucad::ErrorStatus closeDocument(UcApDocument* pAcTargetDocument) = 0;

    virtual int    inputPending(UcApDocument* pAcTargetDocument) = 0;

    virtual Ucad::ErrorStatus disableDocumentActivation() = 0;
    virtual Ucad::ErrorStatus enableDocumentActivation() = 0;
    virtual bool isDocumentActivationEnabled() = 0;

    virtual void executeInApplicationContext(void (*procAddr)(void *), void *pData ) const = 0;

    virtual int documentCount() const = 0;

    virtual void pushResourceHandle( HINSTANCE hNewResourceHandle) = 0;
    virtual void pushUcadResourceHandle() = 0;
    virtual void popResourceHandle() = 0;
    
    virtual Ucad::ErrorStatus sendModelessInterrupt(UcApDocument* pAcTargetDocument) = 0;
};

UcApDocManager* ucDocManagerPtr();
#define ucDocManager ucDocManagerPtr()

inline UcApDocument* curDoc() { return ::ucDocManager->curDocument(); }

#pragma pack (pop)
#endif 

